
// LCR 093.最长的斐波那契子序列
class Solution {
public:
    int lenLongestFibSubseq(vector<int>& arr) {
        int n = arr.size();

        unordered_map<int,int> m;   // 因为数组是严格递增的所以可以先将元素和下标存储起来
        for(int i = 0 ;i < n ;i++)
            m[arr[i]] = i;

        vector<vector<int>> dp(n,vector<int>(n,2));
        int ret = 2;
        for(int i = 2; i < n ;i++)
        {
            for(int j = 1 ; j < i ; j++)
            {
                int prev = arr[i] - arr[j];
                if(m.count(prev) && prev < arr[j]) dp[i][j] = dp[j][m[prev]] + 1;
                ret = max(ret, dp[i][j]);
            }
        }
        return ret == 2 ? 0 :ret;
    }
};